package com.weimei.conversionTreasure.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;

import androidx.core.content.FileProvider;


import com.afollestad.materialdialogs.BuildConfig;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * 分享
 * shiys
 */
public class ShapeUtils {


    public static void onShapePath(Context context , String text){
        if(ValidateUtils.isNotEmptyString(text)){
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, text);
            context.startActivity(Intent.createChooser(intent, "分享"));
        }else{
            ToastUtils.showShort("没有内容");
        }
    }

    // 多文件分享
    public static void onShape(Context context, List<String> paths) {
        if (ValidateUtils.isNotEmptyCollection(paths)) {
            ArrayList<Uri> uris = new ArrayList<Uri>();
            ToastUtils.showShort( "正在打开分享");
            Intent share = new Intent();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                for (int i = 0; i < paths.size(); i++) {
                    File file = new File(paths.get(i));
                    Uri uri = null;
                    try {
                        uri = Uri.parse(MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), file.getName(), null));
                        uris.add(uri);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                for (int i = 0; i < paths.size(); i++) {
                    File file = new File(paths.get(i));
                    uris.add(Uri.fromFile(file));
                }
            }
            share.setAction(Intent.ACTION_SEND_MULTIPLE);//设置分享行为
            share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            share.setType("*/*");//设置分享内容的类型
            share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            share.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
            context.startActivity(Intent.createChooser(share, "分享"));
        } else {
            ToastUtils.showShort("请选择文件");
        }
    }

    // 分享
    public static void onShape(Activity context, String path) {
        File f = new File(path);
        if(FileUtils.isFileExist(f)){
            onShapeFile(context ,f);
        }else{
            onShapePath(context ,path);
        }
    }

    public static void onShape(Activity context, File path) {
        onShapeFile(context ,path);
    }

    // 分享
    private static void onShapeFile(Activity context, File file) {
        ToastUtils.showShort("正在拉起分享...");
        if (null != file && file.exists()) {
            Intent share = new Intent(Intent.ACTION_SEND);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                share.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(context, Authority.aut, file));
            } else {
                share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
            }
            share.setType("*/*");//此处可发送多种文件
            share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            context.startActivity(Intent.createChooser(share, "分享文件"));
        } else {
            ToastUtils.showShort("分享文件不存在");
        }
    }

    public interface Authority {
        String aut = BuildConfig.APPLICATION_ID+".fileprovider";
    }

}
